//
//  ChangePasswordViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/30.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "JTCSM.h"
#import "MyInfoView.h"
#import "GetInfoClass.h"
@interface ChangePasswordViewController ()<UITextFieldDelegate>
{
    MyInfoView *usernameView;
    MyInfoView *oldPasswordView;
    MyInfoView *newPasswordView;
    MyInfoView *surePasswordView;
   
    NSString *username;
    NSString *oldpassword;
    NSString *newpassword;
    NSString *surepassword;
    
    BOOL isChangePassword;
    
    UITextField *theTextfield;

}
@end

@implementation ChangePasswordViewController
- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 提交修改

- (void)tijiao{
    
    
    if ([oldpassword isEqualToString:@""] && [newpassword isEqualToString:@""]&&[surepassword isEqualToString:@""]) {
        isChangePassword = NO;
    }else{
        isChangePassword = YES;
    }
//    User/editPass	{"token":"1","nickname":"2","oldpass":"3","password":"4","password2":"5"}
    LoadData *request = [[LoadData alloc]init];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
    [dict setObject:KTokenString forKey:@"token"];
    [dict setObject:username==nil?@"":username forKey:@"nickname"];
    [dict setObject:oldpassword==nil?@"":oldpassword forKey:@"oldpass"];
    [dict setObject:newpassword==nil?@"":newpassword forKey:@"password"];
    [dict setObject:surepassword==nil?@"":surepassword forKey:@"password2"];
    
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"User/editPass") withDict:dict superView:self.view isAddtag:NO Block:^(id result) {
        
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            if (isChangePassword) {
                [df gotoLoginVC:self.navigationController];
            }else{
                [self gotBack];
            }
            
        }else{
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }
        
    }];

    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    [df isRemoveTarbar:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    username = @"";
    oldpassword = @"";
    newpassword = @"";
    surepassword = @"";
    isChangePassword = NO;
    
    self.navigationItem.rightBarButtonItem = [leftbtn setBarButtonItemsTitle:@"提交" frame:CGRectMake(0, 0, 40, 44) target:self SEL:@selector(tijiao)];
    
    CGFloat viewHeight = 50;
    CGFloat space = 20;
    CGFloat top = 13+KagainChangeHeight;
    [df lineViewWithHeightofTop:top superView:self.view spacing:0 color:RGBACOLOR(241, 241, 241, 1) lineHeight:top];

    usernameView = [[MyInfoView alloc]initWithFrame:CGRectMake(space, top, KCONTROLLERWith-2*space, viewHeight) imagename:nil title:@"昵称"placeholder:nil isShowLabel:textfieldtype radioBtnIndex:0 GroupId:nil];
    [self.view addSubview:usernameView];
    usernameView.textfield.delegate = self;
    [usernameView.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    
    
    oldPasswordView = [[MyInfoView alloc]initWithFrame:CGRectMake(space, KVIEW_Y(usernameView), KCONTROLLERWith-2*space, viewHeight) imagename:nil title:@"原密码"placeholder:nil isShowLabel:textfieldtype radioBtnIndex:0 GroupId:nil];
    [self.view addSubview:oldPasswordView];
    oldPasswordView.textfield.delegate = self;
    oldPasswordView.textfield.placeholder = @"输入原密码";
    oldPasswordView.textfield.secureTextEntry = YES;
    [oldPasswordView.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    
    newPasswordView = [[MyInfoView alloc]initWithFrame:CGRectMake(space, KVIEW_Y(oldPasswordView), KCONTROLLERWith-2*space, viewHeight) imagename:nil title:@"新密码"placeholder:nil isShowLabel:textfieldtype radioBtnIndex:0 GroupId:nil];
    [self.view addSubview:newPasswordView];
    newPasswordView.textfield.delegate = self;
    newPasswordView.textfield.placeholder = @"输入新密码";
    newPasswordView.textfield.secureTextEntry = YES;
    [newPasswordView.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    
    
    surePasswordView = [[MyInfoView alloc]initWithFrame:CGRectMake(space, KVIEW_Y(newPasswordView), KCONTROLLERWith-2*space, viewHeight) imagename:nil title:@"确认新密码"placeholder:nil isShowLabel:textfieldtype radioBtnIndex:0 GroupId:nil];
    [self.view addSubview:surePasswordView];
    surePasswordView.textfield.delegate = self;
    surePasswordView.textfield.placeholder = @"确认新密码";
    surePasswordView.textfield.secureTextEntry = YES;
    [surePasswordView.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    [self getuserinfo];
}

- (void)getuserinfo{
    [[GetInfoClass shareSingle] getinfo];
    [GetInfoClass shareSingle].myblock = ^(UserInfoModel *Model) {
      
        usernameView.textfield.text = Model.nickname;
        username = Model.nickname;
    };
}

#pragma mark 监听textfield 的变化
- (void)valueChanged:(UITextField *)textfield{
    
    
    if (textfield == usernameView.textfield) {
        
        
        username = textfield.text;
        
    }else if (textfield == oldPasswordView.textfield) {
        
        
        oldpassword = textfield.text;
        
    }else if (textfield == newPasswordView.textfield) {
        
        
        newpassword = textfield.text;
        
    }else if (textfield == surePasswordView.textfield) {
        
        
        surepassword = textfield.text;
        
    }
    theTextfield = textfield;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [theTextfield resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
